#!/bin/bash

# 安装tomcat
function setup_tomcat()
{
  echo_start "$1 ... ..."
  confirm_operate "install webserver"
  if [ $? -eq 0 ]; then
    # 停止tomcat
    if [ -d "$HRS_TOMCAT_HOME/bin" ]; then
      if [ -f "$HRS_TOMCAT_HOME/bin/shutdown.sh" ]; then
        kill_proc "HYRENSERVWEB"
        echo_info "stop webserver done."
      fi
    fi

    echo_doing "init env ... ..."
    local HRS_TOMCAT_VERSION="7.0.81"
    if [ -d $HRS_TOMCAT_LOGSDIR ]; then rm -rf $HRS_TOMCAT_LOGSDIR; fi
    if [ -d $HRS_TOMCAT_HOME ]; then rm -rf $HRS_TOMCAT_HOME; fi
    mkdir_check $HRS_TOMCAT_HOME
    mkdir_check $HRS_TOMCAT_LOGSDIR
    echo_done

    echo_doing "unpack ... ..."
    # 解压安装tomcat
    rm -rf /tmp/apache-tomcat-$HRS_TOMCAT_VERSION
    tar zxf $XDIR/packages/apache-tomcat-$HRS_TOMCAT_VERSION.tar.gz -C /tmp/
    mv -f `echo "/tmp/apache-tomcat-$HRS_TOMCAT_VERSION/*"` $HRS_TOMCAT_HOME
    rm -rf $HRS_TOMCAT_HOME/webapps/*
    echo_done

    echo_doing "configure property files ... ..."
    # 使用预定义的server.xml文件替换默认的server.xml。
    # 该文件的应该有如下行：<Connector port="HRSPORT_TOMCAT" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
    cp -f $XDIR/conf/tomcat/server.xml $HRS_TOMCAT_HOME/conf
    sed -i "s/\"HRS_TOMCAT_PORT\"/\"$HRS_TOMCAT_PORT\"/g" $HRS_TOMCAT_HOME/conf/server.xml
    #show_file_forcheck $HRS_TOMCAT_HOME/conf/server.xml
    # 使用预定义的conf/catalina.properties文件。该文件增加了：shared.loader=${catalina.base}/shared,${catalina.base}/shared/*.jar
    cp -f $XDIR/conf/tomcat/catalina.properties $HRS_TOMCAT_HOME/conf/catalina.properties
    #show_file_forcheck $HRS_TOMCAT_HOME/conf/catalina.properties
    # 使用预定义的catalina.sh文件，增加了一行：JAVA_OPTS="-Dtomcatowner=hyren -Xms1024m -Xmx5120m -Xss1024K -XX:PermSize=512m -XX:MaxPermSize=2048m HRS_DARG_HADOOP_NAME"
    cp -f $XDIR/conf/tomcat/catalina.sh $HRS_TOMCAT_HOME/bin/catalina.sh
    if [ "$HYRENPRODUCT_NAME" = "DXG" ]; then
      sed -i "s/HRS_DARG_HADOOP_NAME//" $HRS_TOMCAT_HOME/bin/catalina.sh
    elif [ "$HYRENPRODUCT_NAME" = "DSP" ] || [ "$HYRENPRODUCT_NAME" = "GOF" ]; then
      #sed -i "s%HRS_DARG_HADOOP_NAME%-Djava\.library\.path=\/opt\/cloudera\/parcels\/CDH\/lib\/hadoop\/lib\/native%" $HRS_TOMCAT_HOME/bin/catalina.sh
      sed -i "s%HRS_DARG_HADOOP_NAME%-D$HRS_DARG_HADOOP_NAME%" $HRS_TOMCAT_HOME/bin/catalina.sh
      grep "$HRS_DARG_HADOOP_NAME" $HRS_TOMCAT_HOME/bin/catalina.sh > /dev/null || { die "seded HRS_DARG_HADOOP_NAME value fail in $HRS_TOMCAT_HOME/bin/catalina.sh"; }
    fi
    # 修改catalina.sh中配置的日志输出目录。因为$HYRENDATA_DATA里面有斜线需要转义，所以使用%
    sed -i "s%HRS_CATALINA_OUT%$HRS_TOMCAT_LOGSDIR\/catalina\.out%g" $HRS_TOMCAT_HOME/bin/catalina.sh
    grep -w "$HRS_TOMCAT_LOGSDIR/catalina.out" $HRS_TOMCAT_HOME/bin/catalina.sh > /dev/null || { die "seded HRS_TOMCAT_LOGSDIR value fail in $HRS_TOMCAT_HOME/bin/catalina.sh"; }
    #show_file_forcheck $HRS_TOMCAT_HOME/bin/catalina.sh

    # 修改tomcat log(conf/logging.properties)
    cp -f $XDIR/conf/tomcat/logging.properties $HRS_TOMCAT_HOME/conf/logging.properties
    #sed -i "s/1catalina.org.apache.juli.FileHandler.directory/1catalina.org.apache.juli.FileHandler.directory=$HYRENDATA_DATA\/logs\/tomcat\/logs/g" $HRS_TOMCAT_HOME/conf/logging.properties
    #sed -i "s/2localhost.org.apache.juli.FileHandler.directory/2localhost.org.apache.juli.FileHandler.directory=$HYRENDATA_DATA\/logs\/tomcat\/logs/g" $HRS_TOMCAT_HOME/conf/logging.properties
    #sed -i "s/3manager.org.apache.juli.FileHandler.directory/3manager.org.apache.juli.FileHandler.directory=$HYRENDATA_DATA\/logs\/tomcat\/logs/g" $HRS_TOMCAT_HOME/conf/logging.properties
    #sed -i "s/4host-manager.org.apache.juli.FileHandler.directory/4host-manager.org.apache.juli.FileHandler.directory=$HYRENDATA_DATA\/logs\/tomcat\/logs/g" $HRS_TOMCAT_HOME/conf/logging.properties
    sed -i "s%\${catalina\.base}\/logs%$HRS_TOMCAT_LOGSDIR%g" $HRS_TOMCAT_HOME/conf/logging.properties
    grep -w "$HRS_TOMCAT_LOGSDIR" $HRS_TOMCAT_HOME/conf/logging.properties > /dev/null || { die "seded HRS_TOMCAT_LOGSDIR value fail in $HRS_TOMCAT_HOME/conf/logging.properties"; }
    #show_file_forcheck $HRS_TOMCAT_HOME/conf/logging.properties
    echo_done

    echo_doing "deliver jar files ... ..."
    # 建立shared依赖包
    # fenfa是集群内需要的.shared下和agent下基本一样，agent下会多2-3个，fenfa下只有6-7个左右包，是跑hbase中yarn需要的包
    mkdir_check $HRS_TOMCAT_HOME/shared
    cp $HYREN_USERPD_TOMCATSHARED/*.jar $HRS_TOMCAT_HOME/shared
    echo_done

    # hadoop的配置文件
    mkdir_check $HRS_TOMCAT_HOME/bin/conf
    cp $HADOOP_FILES_DIR/conf/*.xml $HRS_TOMCAT_HOME/bin/conf || { die "cp fail! [$HADOOP_FILES_DIR/conf/*.xml] to [$HRS_TOMCAT_HOME/bin/conf]"; }
    if [ ! -f $HRS_TOMCAT_HOME/bin/conf/core-site.xml ] || [ ! -f $HRS_TOMCAT_HOME/bin/conf/hbase-site.xml ] || [ ! -f $HRS_TOMCAT_HOME/bin/conf/hdfs-site.xml ] || [ ! -f $HRS_TOMCAT_HOME/bin/conf/mapred-site.xml ] || [ ! -f $HRS_TOMCAT_HOME/bin/conf/yarn-site.xml ]; then
      die "Must include files ( core-site.xml, hbase-site.xml, hdfs-site.xml, mapred-site.xml, yarn-site.xml ) in [$HRS_TOMCAT_HOME/bin/conf]"
    fi
    grep ",$HRS_FENFA_HOME/*" $HRS_TOMCAT_HOME/bin/conf/yarn-site.xml > /dev/null || { die "illegle [$HRS_TOMCAT_HOME/bin/conf/yarn-site.xml], yarn.application.classpath missing : ',$HRS_FENFA_HOME/*'"; }

    echo_doing "set dir property ... ..."
    chown -R $HYREN_USER:$HYREN_USER $HRS_TOMCAT_HOME
    chown -R $HYREN_USER:$HYREN_USER $HRS_TOMCAT_LOGSDIR
    echo_done

    su - $HYREN_USER -s /bin/bash $XDIR/util/tomcat_suser.sh
    die_insh_check

    # 使用海云用户，启动 tomcat
    echo_info "start webserver ... ..."
    su - $HYREN_USER -s /bin/bash $HRS_TOMCAT_HOME/bin/startup.sh
    if [ $? -ne 0 ]; then
      die "startup webserver fail! "
    fi
    
    waiting_proc_status $HRS_TOMCAT_PORT 0 "web start"
    sleep 1
    local nums=0
    while true; do
      # 有这一行表明已经启动成功
      grep "Server startup in [0-9]* ms" $HRS_TOMCAT_LOGSDIR/catalina.out > /dev/null
      if [ $? -eq 0 ]; then
        break;
      fi
      nums=$(expr $nums + 1 )
      if [ $nums -gt 600 ]; then
        die "start webserver Fail! check logfile : $HRS_TOMCAT_LOGSDIR/catalina.out"
      else
        echo_doing "."
      fi
    done
    unset nums
    echo_info "start webserver ... ... done."

    # 设置环境变量
    #set_bash_profile "HRS TOMCAT ENV" "HRSCOMMENT" "$HYREN_USER"
    set_bash_profile "HRS_TOMCAT_HOME" "$HRS_TOMCAT_HOME" "$HYREN_USER"
    set_bash_profile "HRS_TOMCAT_LOGSDIR" "$HRS_TOMCAT_LOGSDIR" "$HYREN_USER"
    set_bash_profile "HRS_TOMCAT_PORT" "$HRS_TOMCAT_PORT" "$HYREN_USER"

    echo_success "$1"
    echo_info "****************************************"
    echo_info "WEBRUN HOME   dir  : $HRS_TOMCAT_HOME"
    echo_info "WEBRUN LOGS   file : $HRS_TOMCAT_LOGSDIR"
    echo_info "WEBRUN SERVER PORT : $HRS_TOMCAT_PORT"
    echo_info "****************************************"
    if grep "Exception" $HRS_TOMCAT_LOGSDIR/catalina.out > /dev/null; then
      echo_warn "Somthing wrong on webserver, please check log in $HRS_TOMCAT_LOGSDIR"
    fi

    echo_passed "$1 done."
  else
    echo_passed "$1 SKIP"
  fi
}
